/*
    板子硬件情况定义，统一提供给APP或者其他组件的名称
    GPIO的相关设置、模式切换建议定义在这里
*/
#ifndef _HARD_WIRE_H_
#define _HARD_WIRE_H_

#include "mcu\HC32\HC32_LyLib.h"



#define UART_PIN_INIT           UART_PIN_DEINIT// DEBUG 时不能开启UART端口（UART端口共用了SWD）

// 切换回SWD端口
#define UART_PIN_DEINIT    _SYSCTRL_UNLOCK;M0P_SYSCTRL->SYSCTRL1 = 0X00000008;//SYSCTRL_SWD_USE_SWD;

#define LED_PIN_INIT		    \
{   \
    GPIO_P03_SEL_PCA_CH3;\
    GPIO_P25_SEL_PCA_CH0;\
    GPIO_P33_SEL_PCA_CH1;\
    GPIO_DIR_OUTPUT(0, 3);  \
    GPIO_DIR_OUTPUT(2, 5);  \
    GPIO_DIR_OUTPUT(3, 3);  \
}
#define LED_PWM_INIT        Pca_Init();
#define _SET_LED_R(r)       Pca_SetPWMValue(0,r)
#define _SET_LED_G(g)       Pca_SetPWMValue(3,g)
#define _SET_LED_B(b)       Pca_SetPWMValue(1,b)

#define BUTTON_PIN          M0P_GPIO->P2IN_f.P26
#define ENABLE_KEY_INT      {GPIO_FIE_ENABLE(2, 6); NVIC_EnableIRQ(PORT2_IRQn);}
#define BUTTON_PIN_INIT     {GPIO_DIR_INPUT(2,6); GPIO_PU_ENABLE(2,6); ENABLE_KEY_INT;}

#define MIC_PIN             M0P_GPIO->P1IN_f.P14
#define MIC_PIN_INIT        {GPIO_DIR_INPUT(1,4); GPIO_PD_ENABLE(1,4);GPIO_RIE_ENABLE(1, 4);NVIC_EnableIRQ(PORT1_IRQn);}

#define TEN_PIN             M0P_GPIO->P0OUT_f.P01
#define TEN_ON              TEN_PIN = 0
#define TEN_OFF             TEN_PIN = 1
#define TEN_PIN_INIT        {GPIO_DIR_OUTPUT(0, 1);TEN_ON;}

#define DCIN_PIN            M0P_GPIO->P2IN_f.P24
#define ENABLE_DCIN_INT     {GPIO_RIE_ENABLE(2, 4); /*NVIC_EnableIRQ(PORT2_IRQn);*/}
#define DCIN_PIN_INIT       {GPIO_DIR_INPUT(2, 4); ENABLE_DCIN_INT;}

#define ABFBV_PIN_INIT      \
{\
    GPIO_ADS_ANALOG(0, 2); \
    GPIO_DIR_INPUT(0, 2);\
}

#define ASVP_PIN_INIT      \
{\
    GPIO_ADS_ANALOG(3, 4); \
    GPIO_DIR_INPUT(3, 4);\
}

#define ASDCC_PIN_INIT      \
{\
    GPIO_ADS_ANALOG(3, 5); \
    GPIO_DIR_INPUT(3, 5);\
}


#define PWM_PIN_INIT    \
{\
    GPIO_P36_SEL_TIM6_CHA;\
    GPIO_DIR_OUTPUT(3, 6);\
    GPIO_P32_SEL_TIM6_CHB;\
    GPIO_DIR_OUTPUT(3, 2);\
}

#define BST_PIN_INIT  \
{\
    M0P_GPIO->P1OUT_f.P15 = 1;\
    GPIO_DIR_OUTPUT(1, 5);\
    M0P_GPIO->P2OUT_f.P23 = 0;\
    GPIO_DIR_OUTPUT(2 ,3);\
}
#define PRV_BST_PIN_SEL_PWM     GPIO_P23_SEL_TIM4_CHA
#define PRV_BST_PIN_SEL_GPIO    GPIO_P23_SEL_GPIO_P23
#define MAIN_BST_PIN_SEL_PWM    GPIO_P15_SEL_TIM4_CHB
#define MAIN_BST_PIN_SEL_GPIO   GPIO_P15_SEL_GPIO_P15

#define HW_LOW_POWER_STATE      \
{\
    /* 端口模式 0输出 1输入*/\
	M0P_GPIO->P0DIR = 0XFFFFFFFF;           /* 1111 1111 1111 1111*/\
	M0P_GPIO->P1DIR = 0XFFFFFFFF;\
	M0P_GPIO->P2DIR = 0XFFFFFFFF;\
	M0P_GPIO->P3DIR = 0XFFFFFFFF;\
	/* 所有端口数字模式*/\
	M0P_GPIO->P0ADS = 0;\
	M0P_GPIO->P1ADS = 0;\
	M0P_GPIO->P2ADS = 0;\
	M0P_GPIO->P3ADS = 0;\
	/* 上拉配置 1开启*/\
	M0P_GPIO->P0PU = 0X0000FFFF;			/* 1111 1111 1111 1111*/\
	M0P_GPIO->P1PU = 0X0000FFEF;			/* 1111 1111 1110 1111*/\
	M0P_GPIO->P2PU = 0X0000FFE7;			/* 1111 1111 1110 0111*/\
	M0P_GPIO->P3PU = 0X0000FF9B;			/* 1111 1111 1001 1011*/\
	/* 下拉配置 1开启*/\
	M0P_GPIO->P0PD = 0X00000000;			/* 0000 0000 0000 0000*/\
	M0P_GPIO->P1PD = 0X00000010;			/* 0000 0000 0001 0000*/\
	M0P_GPIO->P2PD = 0X00000008;			/* 0000 0000 0000 1000*/\
	M0P_GPIO->P3PD = 0X00000044;			/* 0000 0000 0100 0100*/\
    /* 关闭ADC 和 BGR*/\
    M0P_ADC->CR0_f.ADCEN = 0;\
	M0P_BGR->CR = 0;\
}

// Board_Init 已经设置的内容无需再重复
#define HW_NORMAL_STATE     \
{\
    /* 开启ADC和BGR*/\
    M0P_BGR->CR = 0x03;\
    M0P_ADC->CR0_f.ADCEN = 1;\
}



#define ADCH_VBAT       _ADC_CH_VDD_3


// VBAT压降，电池电压到检测管脚之间的一个固定压降
#define VBAT_DROP       0

#endif

